package gold.digger;

import gold.utils.InputUtil;

import java.util.Arrays;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC1706 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int[] findBall(int[][] grid) {
            int m = grid.length, n = grid[0].length;
            int[] ans = new int[n];
            for (int i = 0; i < n; i++) {
                int curLocation = i;
                for (int j = 0; j < m; j++) {
                    // 网格中用 1 表示 \
                    // 网格中用 -1 表示 /
                    if (grid[j][curLocation] == 1) {
                        // 形状是 \/
                        if (curLocation == n - 1 || grid[j][curLocation + 1] == -1) {
                            curLocation = -1;
                            break;
                        }

                        // 形状是 \\
                        if (grid[j][curLocation + 1] == 1) {
                            curLocation++;
                            continue;
                        }
                    } else {
                        // 形状是 \/
                        if (curLocation == 0 || grid[j][curLocation - 1] == 1) {
                            curLocation = -1;
                            break;
                        }

                        // 形状是 //
                        if (grid[j][curLocation - 1] == -1) {
                            curLocation--;
                            continue;
                        }
                    }
                }
                ans[i] = curLocation;
            }

            return ans;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(Arrays.toString(solution.findBall(InputUtil.toDoubleIntegerArray("[[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]"))));
        System.out.println(Arrays.toString(solution.findBall(InputUtil.toDoubleIntegerArray("[[-1]]"))));
        System.out.println(Arrays.toString(solution.findBall(InputUtil.toDoubleIntegerArray("[[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]]"))));
    }

    public static void main(String[] args) throws Exception {
        LC1706 an = new LC1706();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
